const express = require('express');
const router = express.Router();
const {chatRoomMsg} = require('../app');
const Blog = require('../model/blog')
const User = require('../model/user')
const Message = require('../model/message')

// 储存记录
router.post('/saveChat', async (req,res) => {
  var reqData = {msg:'储存成功',status:0}
  var uid = req.body.uid;
  var toUid = req.body.toUid;
  var content = req.body.content;
  var isHaveUid = true
  var isHaveToUid = true
  try{
    await User.findOne({_id:uid})
  }catch(e){
    isHaveUid = false
    reqData.msg = '发送者不存在'
    reqData.status = 1
  }
  try{
    await User.findOne({_id:toUid})
  }catch(e){
    isHaveToUid = false
    reqData.msg = '接受者不存在'
    reqData.status = 1
  }
  if(isHaveToUid && isHaveUid){
    await Message.findOne({uid:uid}).then(async function(data){
      // console.log(data)
      if(data === null){
        // console.log(data._id);
        var message = new Message({
          uid: uid,
          belong: {
            toUid:toUid,
            contentList:{
              content:content,
              time:Date.now(),
              userid: uid
            },
          }
        })
        return message.save();
      }
      else{
        var arr = data.belong
        var flag = true
        for(let i = 0; i < data.belong.length; i++){
          if(data.belong[i].toUid == toUid){
            flag = false
            arr[i].contentList.push({
              content:content,
              time:Date.now(),
              userid: uid
            })
            // console.log('aaa',arr)
            await Message.findOneAndUpdate({uid:uid},{belong:arr},{new: true})
            return
          }
        }
        if(flag){
          arr.push({
            toUid:toUid,
            contentList:{
              content:content,
              time:Date.now(),
              userid: uid
            }
          })
          // console.log('aaa',arr)
          await Message.findOneAndUpdate({uid:uid},{belong:arr},{new: true})
        }
      }
    })
  }
  res.json(reqData,201);
})
// 查看记录
router.post('/getChat', async (req,res) => {
  var reqData = {msg:'查询成功',status:0}
  var uid = req.body.uid;
  var toUid = req.body.toUid;
  var isHaveUid = true
  var isHaveToUid = true
  try{
    await User.findOne({_id:uid})
  }catch(e){
    isHaveUid = false
    reqData.msg = '发送者不存在'
    reqData.status = 1
  }
  try{
    await User.findOne({_id:toUid})
  }catch(e){
    isHaveToUid = false
    reqData.msg = '接受者不存在'
    reqData.status = 1
  }
  if(isHaveToUid && isHaveUid){
    try{
      await Message.findOne({uid:uid}).then((data) => {
        for(let i = 0; i < data.belong.length; i++){
          if(data.belong[i].toUid == toUid){
            reqData.userSayList = data.belong[i].contentList
            console.log(reqData.userSayList)
            return
          }
        }
      })
    }
    catch(e){
      reqData.userSayList = []
    }


    try{
      await Message.findOne({uid:toUid}).then((data) => {
        for(let i = 0; i < data.belong.length; i++){
          if(data.belong[i].toUid == uid){
            reqData.toUserSayList = data.belong[i].contentList
            console.log(reqData.toUserSayList)
            return
          }
        }
      })
    }catch(e){
      reqData.toUserSayList = []
    }
  }
  res.json(reqData,201);
})

router.get('/test',(req,res) => {
  var uid = req.body.uid;
  var title = req.body.title;
  var content = req.body.content;
  console.log('uid,title,content:',uid,title,content)
  User.findOne({username:'424242'}).then(function(data){
    console.log(data._id);
    var blog = new Blog({
      uid: uid,
      blog: {
        title:title,
        content:content
      }
    })
    return blog.save();
  })

  // blog(req.request.body).save()
  res.json({data:'test'},201);
})


module.exports = router;